package com.hczoop.utils.file;

import java.text.DecimalFormat;

import com.hczoop.utils.web.StringUtil;

public class NumberFormatUtil {
	private static final DecimalFormat df2 = new DecimalFormat("#0.00");
	private static final DecimalFormat df3 = new DecimalFormat("#0.000");
	
	public static String keep2DecimalPlaces(String num){
		if( ! StringUtil.isEmpty(num) && num.matches("^\\d+[.]?[\\d]*$")  ){
			Double d = Double.parseDouble(num);
			return df2.format(d.doubleValue());
		}
		return "";
	}
	
	public static Double keep2DecimalPlaces(Double num){
		if(num != null){
			df2.format(num.doubleValue());
		}
		return 0d;
	}
	
	
	public static String keep3DecimalPlaces(String num){
		if( ! StringUtil.isEmpty(num) && num.matches("^\\d+[.]?[\\d]*$")  ){
			Double d = (Double.parseDouble(num));
			return df3.format(d.doubleValue());
		}
		return "";
	}
	
	public static Double keep3DecimalPlaces(Double num){
		if(num != null){
			String str = df3.format(num.doubleValue());
			return Double.parseDouble(str);
		}
		return 0d;
	}
	
}
